
console.log("content init")
const CONTENT_SELECTOR = "#root > div > div.c3ecdb44 > div.f2eea526 > div > div.b83ee326 > div > div > div.dad65929"
const INPUT_SELECTOR = ".ec4f5d61"
addDownloadIcon();
observeChanges();
function generateImg(element) {
    console.log("开始生成图片");
    const padding = element.style.padding;
    element.style.padding = "20px"
    html2canvas(element, {
        backgroundColor: "#292a2d",
    }).then(canvas => {
        element.style.padding = padding;
        const link = document.createElement("a");
        link.download = "deepseek.png";
        link.href = canvas.toDataURL("image/png");
        link.click();
    });
    console.log("结束生成图片");
}
function generatePdf(element) {
    console.log("开始生成PDF");
    // const backgroundColor = element.style.backgroundColor;
    element.style.backgroundColor = "#292a2d"
    // const padding = element.style.padding;
    // element.style.padding = "20px"

    html2pdf().set({
        margin: 1,
        filename: 'deepseek.pdf',
        image: { type: 'jpeg', quality: 0.98 },
        html2canvas: { scale: 1 },
        jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
    }).from(element).save();
    // element.style.padding = padding;
    // element.style.backgroundColor = backgroundColor;
    console.log("结束生成PDF");
}
function generate() {
    let element = document.querySelector(CONTENT_SELECTOR);
    if (element == null) {
        return;
    }
    chrome.storage.sync.get("type", ({ type }) => {
        if(type === "IMAGE") {
            generateImg(element);
            return;
        }
        if(type === "PDF") {
            generatePdf(element);
            return;
        }
    });
}
function addDownloadIcon() {
    const element = document.querySelector(INPUT_SELECTOR);
    if (element == null) {
        console.log("找不到对话框");
        return;
    }
    //判断img是否已经存在
    const downloadIcon = document.querySelector("#download_ai_share");
    if (downloadIcon) {
        return;
    }
    const img = document.createElement("img");
    img.style.marginLeft = "0.9em";
    img.src = chrome.runtime.getURL("images/download.png");
    img.onclick = generate;
    img.title = "下载对话";
    img.id = "download_ai_share";
    img.addEventListener('mouseenter', function () {
        img.classList.add('hovered');
    });
    img.addEventListener('mouseleave', function () {
        img.classList.remove('hovered');
    });

    img.onclick = generate;
    element.appendChild(img)
}

function observeChanges() {

    const targetNode = document.querySelector("#root");
    const config = { childList: true, subtree: true };

    const callback = function (mutationsList, observer) {
        for (let mutation of mutationsList) {
            if (mutation.type === 'childList') {
                console.log("page status changed")
                addDownloadIcon();
            }
        }
    };
    const observer = new MutationObserver(callback);
    observer.observe(targetNode, config);
}
